package org.cmroot.certmaster.client.dialogs;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import org.cmroot.certmaster.client.GWTClientService;
import org.cmroot.certmaster.client.widgets.ErrorLabel;
import org.cmroot.certmaster.client.widgets.OpensslText;
import org.cmroot.certmaster.shared.dto.X509DTO;
import org.cmroot.certmaster.shared.dto.ext.Pkcs10ExtDTO;

import java.util.List;

/**
 * Copyright (c) 2011 Andrey Domas <andrey.domas at gmail.com>.
 * *
 * This file is part of certmaster.
 * *
 * Certmaster is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * *
 * Certmaster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see http://www.gnu.org/licenses
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301  USA
 */
public class SignDialog extends Composite {

    interface SignDialogUiBinder extends UiBinder<Widget, SignDialog> {}
    private static SignDialogUiBinder _signDialogUiBinder = GWT.create(SignDialogUiBinder.class);

    @UiField DialogBox dialogBox;
    @UiField PushButton signButton;
    @UiField PushButton cancelButton;
    @UiField ListBox caList;
    @UiField SimplePanel opensslText_panel;
    @UiField ErrorLabel errorLabel;
    OpensslText opensslText = new OpensslText(errorLabel);
    Long reqId;

    @UiConstructor
    public SignDialog(Long reqId) {
        initWidget(_signDialogUiBinder.createAndBindUi(this));
        opensslText_panel.add(opensslText);
        dialogBox.center();
        this.reqId = reqId;

        cancelButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                dialogBox.hide();
            }
        });

        signButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                dialogBox.hide();
            }
        });
    }

    public void addConfirmHandler(ClickHandler handler) {
        signButton.addClickHandler(handler);
    }

    private void appendCAList() {
        GWTClientService.App.getInstance().getCA(new AsyncCallback<List<X509DTO>>() {
            @Override
            public void onFailure(Throwable throwable) {
                errorLabel.showError(throwable.getMessage());
            }

            @Override
            public void onSuccess(List<X509DTO> x509DTOs) {
                for (X509DTO ca : x509DTOs) {
                    caList.addItem(ca.getCN(), ca.getId().toString());
                }
            }
        });
    }

    public void show() {
        appendCAList();
        opensslText.showReq(reqId);
        dialogBox.show();
    }
}